package ZuoShen;


public class MatricToGraph {
    public static Graph matirxToGraph(int[][] matirx){
        Graph graph = new Graph();
        for (int i = 0; i < matirx.length; i++) {
            Node from = new Node(i);
            Graph.nodes.put(i, from);
            for (int j = 0; j < matirx.length; j++) {
                if (i != j && matirx[i][j] != -1) {
                    Node to = new Node(j);
                    Graph.nodes.put(j, to);
                    Edge edge = new Edge(from, to, matirx[i][j]);
                    Graph.edges.add(edge);
                    from.edges.add(edge);
                    from.nexts.add(to);
                }
            }
        }
        return graph;
    }

    public static void main(String[] args) {
        int n = 5;
        int[][] matirx = {
                {-1, 2, 3, -1, 6},
                {2, -1, -1, 4, 2},
                {3, -1, -1, -1, -1},
                {-1, 4, -1, -1, 7},
                {6, 2, -1, 7, -1}
        };

        Graph graph = new Graph();
        for (int i = 0; i < n; i++) {
            Node from = new Node(i);
            Graph.nodes.put(i, from);
            for (int j = 0; j < n; j++) {
                if (i != j && matirx[i][j] != -1) {
                    Node to = new Node(j);
                    Graph.nodes.put(j, to);
                    Edge edge = new Edge(from, to, matirx[i][j]);
                    Graph.edges.add(edge);
                    from.edges.add(edge);
                    from.nexts.add(to);
                }
            }
        }
    }
}
